﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using uk.org.aspellclark.todolistppc;
using System.IO;

namespace ConsoleApplication1
{
    class ToDoList
    {
        string m_strFile = "";
        bool DataChanged = true;
        string m_sFileFormat;
        string m_sProjectName;
        string m_iNextUniqueID;
        string m_dtEarliestDueDate;
        string m_sFileVersion;
        string m_sLastModified;



        public void writeTodoListFile(string FileName,List<SyncTask> Tasks)
        {
            IQueryable<SyncTask> tasks = Tasks.AsQueryable();
            m_strFile = FileName;
            if (m_strFile == "")
            {
                m_strFile = "TodoList.xml";
            }
            if (DataChanged == true)
            {
                if (File.Exists(m_strFile))
                {
                    if (File.Exists(m_strFile + ".bak"))
                    {
                        File.Delete(m_strFile + ".bak");
                    }
                    File.Move(m_strFile, m_strFile + ".bak");
                }
                XmlTextWriter xmlWriter = new XmlTextWriter(m_strFile, System.Text.Encoding.GetEncoding("Windows-1252"));

                xmlWriter.Formatting = Formatting.Indented;
                xmlWriter.Indentation = 0;
                xmlWriter.IndentChar = ' ';

                xmlWriter.WriteStartDocument();


                    //write out the root element - Project
                    xmlWriter.WriteStartElement("TODOLIST");

                    xmlWriter.WriteAttributeString("", "FILEFORMAT", "", m_sFileFormat);
                    xmlWriter.WriteAttributeString("", "PROJECTNAME", "", m_sProjectName);
                    xmlWriter.WriteAttributeString("", "NEXTUNIQUEID", "", "");//m_iNextUniqueID.ToString()
                    xmlWriter.WriteAttributeString("", "EARLIESTDUEDATE", "", m_dtEarliestDueDate);

                    int iFileVer = Convert.ToInt32(m_sFileVersion);
                    iFileVer++;
                    m_sFileVersion = iFileVer.ToString();
                    xmlWriter.WriteAttributeString("", "FILEVERSION", "", m_sFileVersion);

                    m_sLastModified = DateTime.Now.ToString("yyyy-mm-dd");
                    xmlWriter.WriteAttributeString("", "LASTMODIFIED", "", m_sLastModified);


                    //write out the tree nodes
                    IQueryable<SyncTask> basetasks = Tasks.Where(t => !(t.ParentTaskId > 0)).AsQueryable();
                    int count = basetasks.Count();
                    int totaltasks = Tasks.Count;
                    foreach (SyncTask tmpNode in basetasks)
                    {
                        OutputNodes(xmlWriter, tmpNode, Tasks.AsQueryable());
                    }

                    xmlWriter.WriteEndElement();
                    xmlWriter.WriteEndDocument();
                    xmlWriter.Flush();
                    xmlWriter.Close();
                    DataChanged = false;

                    //m_lblProject.Text = m_sProjectName + " [v" + m_sFileVersion + " - " + m_sLastModified + "]";
            }//if (DataChanged)
        }

        private void OutputNodes(XmlTextWriter pXmlWriter, SyncTask pNode, IQueryable<SyncTask> Tasks)
        {
            pNode.writeToXml(pXmlWriter);
            #region Add Children
            foreach (SyncTask tmpNode in Tasks.Where(t => t.ParentTaskId == pNode.TaskId))
            {
                OutputNodes(pXmlWriter, tmpNode, Tasks);
            }
            #endregion
            pNode.endWriteToXml(pXmlWriter);
        }
    }
}
